<template>
  <div class="setting-dialog">
    <el-dialog
      v-model="dialogVisible"
      width="550"
      :modal="false"
      align-center
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :destroy-on-close="true"
      @closed="closedHandle"
    >
      <template #header>
        <dialog-title />
      </template>
      <div class="content">
        <div class="setting-title">
          通用设置:
        </div>
        <el-checkbox v-model="closeAndHide">
          最小化到系统托盘
        </el-checkbox>
        <div
          class="setting-title"
          style="margin-top: 20px;"
        >
          下载路径:
        </div>
        <div class="setting-output">
          <el-input
            v-model="downloadPath"
            readonly
            class="setting-output-input"
          />
          <el-button
            style="background:transparent"
            @click="selectPath"
          >
            预览
          </el-button>
        </div>
      </div>
      <template #footer>
        <div class="dialog-footer">
          <el-checkbox v-model="isDefaultPath">
            默认此路径为下载路径
          </el-checkbox>
          <el-button
            type="primary"
            class="button"
            style="width:116px;border-radius: 18px;flex-grow: 0;"
            @click="handleCancelInner('cancel')"
          >
            确定
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { storeToRefs } from 'pinia'
import { invokeAsyncAPi } from '@/utils/xz-fn'
import { useDownloadPath } from '@/stores/download-path'

const props = defineProps({
  handleSuccess: {
    type: Function,
    default:()=>{}
  },
  handleCancel: {
    type: Function,
    default:()=>{}
  }
})

const dialogVisible = ref(true)
const clickType = ref('cancel')
const cancelType = ref('cancel')

const downloadPathStore = useDownloadPath();
const {downloadPath, isDefaultPath ,closeAndHide}=storeToRefs(downloadPathStore)

const handleCancelInner = (_cancelType: string) => {
  clickType.value = 'cancel'
  cancelType.value = _cancelType
  dialogVisible.value = false
}

const closedHandle = () => {
  if (clickType.value === 'success') {
    props.handleSuccess()
  } else {
    props.handleCancel(cancelType.value)
  }
}
const selectPath = async () => {//选择路径
  const rs=await invokeAsyncAPi('fq_BrowserAndSelectFile', {
    pick_folders:true
  });
  if (rs.ret) {
    const path = rs.files[0];
    downloadPathStore.setDownloadPath(path);
  }
}
</script>

<style scoped lang="scss">
.setting-dialog {
  .content{
    background: rgba(25,38,85,0.05);
    border-radius: 6px;
    padding:14px;
    .setting-title{
      font-family: Microsoft YaHei, Microsoft YaHei;
      font-weight: bold;
      font-size: 12px;
      color: #192655;
      margin-bottom: 5px;
    }
    .setting-output{
      display: flex;
      align-items: center;
      gap:15px;
      .setting-output-input{
        :deep(.el-input__wrapper){
          background:transparent;
        }
        width:316px;
      }
    }
  }
  
  .dialog-footer{
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding:0 15px ;
  }
}
</style>
